package cn.ecut.ajax;

import cn.ecut.javabean.User;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/ajax")
public class AjaxServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Enumeration<String> parameterNames = request.getParameterNames();

        while( parameterNames.hasMoreElements() ){
            String name = parameterNames.nextElement() ;
            String header = request.getParameter(name);
            System.out.println( "ParameterName : " + name + " , ParameterValue ： " + header );
        }


        User user = new User() ;
        user.setPassword( request.getParameter("password"));
        user.setName( request.getParameter("username"));

        Map<String , Object> map = new HashMap<>() ;
        map.put( "data" , user ) ;
        // 状态如果是 0 ，那么就 表示成功
        map.put( "status" , 0 ) ;
        map.put("message" , "成功" ) ;

        // 需要将 map 转成 JSON
        String s = JSON.toJSONString(map);

        response.getWriter().write( s );

    }
}
